package cn.itsource.ymcc;

import lombok.Data;

import java.lang.reflect.Field;
import java.util.*;

//返回JSON结果
@Data
public class JSONResult2 {

    private boolean success = true;

    private String message = "成功";

    //错误码，用来描述错误类型 ，1000 表示么有错误
    private String code = "1000";

    //返回的数据
    private Object data;

    public static void main(String[] args) {
        User user = new User();
        user.setAge(18);
        user.setUsername("Zs");

        User user2 = new User();
        user2.setAge(18);
        user2.setUsername("Zs");
        JSONResult2 jsonResult2 = new JSONResult2();
        List<User> users = Arrays.asList(user, user2);
        jsonResult2.datas(users);

    }

    //表头
    private Map<String,String> header = new HashMap<>();

    //列表
    private Map<String, List<Object>> datas = new  HashMap<>();

    public void datas(List objs){
        Object obj = objs.get(0);
        //处理表头===================================================
        Class<?> cls = obj.getClass();
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            //注解
            FieldAnno anno = declaredField.getAnnotation(FieldAnno.class);
            //字段名
            String name = declaredField.getName();
            if(anno != null){
                //中文名
                String value = anno.value();
                header.put(name,value);
            }else{
                header.put(name,name);
            }
        }
        //处理表头===================================================
        for(Object o:objs){
            Class<?> clss = obj.getClass();

            //一个对象的n个字段
            Field[] declaredFieldss = cls.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                //字段名字
                String name = declaredField.getName();
                List<Object> objects = datas.get(name);
                if(objects == null || objects.isEmpty()){
                    objects = new ArrayList<>();
                    datas.put(name,objects);
                }
                try {
                    declaredField.setAccessible(true);
                    Object value = declaredField.get(o);
                    objects.add(value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println(header);
        System.out.println(datas);
        System.out.println("========");
    }


    /** 创建当前实例 **/
    public static JSONResult2 success(){
        return new JSONResult2();
    }
    /** 创建当前实例 **/
    public static JSONResult2 success(Object obj){
        JSONResult2 instance = new JSONResult2();
        instance.setData(obj);
        return instance;
    }

    public static JSONResult2 success(Object obj, String code){
        JSONResult2 instance = new JSONResult2();
        instance.setCode(code);
        instance.setData(obj);
        return instance;
    }
    /** 创建当前实例 **/
    public static JSONResult2 error(){
        JSONResult2 jsonResult = new JSONResult2();
        jsonResult.setSuccess(false);
        return jsonResult;
    }

    /** 创建当前实例 **/
    public static JSONResult2 error(String message){
        JSONResult2 instance = new JSONResult2();
        instance.setMessage(message);
        instance.setSuccess(false);
        return instance;
    }
    public static JSONResult2 error(String message, String code){
        JSONResult2 instance = new JSONResult2();
        instance.setMessage(message);
        instance.setSuccess(false);
        instance.setCode(code);
        return instance;
    }

}
